/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.storage.service;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.storage.entity.Activity;
import com.jeesite.modules.storage.entity.Banner;
import com.jeesite.modules.storage.entity.Route;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.storage.entity.Plate;
import com.jeesite.modules.storage.dao.PlateDao;

/**
 * 模块Service
 */
@Service
@Transactional(readOnly=true)
public class PlateService extends CrudService<PlateDao, Plate> {

	@Autowired
	private ActivityService activityService;

	@Autowired
	private RouteService routeService;

	@Autowired
	private PlateItemService plateItemService;

	/*
	 * 处理联动
	 * */
	void handleCascade(Plate entity){
		if(entity == null)
		{
			return;
		}
		entity.setActivity(StringUtils.isBlank(entity.getActivityId()) ? null : activityService.get(entity.getActivityId()));
		entity.setRoute(StringUtils.isBlank(entity.getRouteId()) ? null : routeService.get(entity.getRouteId()));
	}

	/**
	 * 获取单条数据
	 * @param plate
	 * @return
	 */
	@Override
	public Plate get(Plate plate) {
		Plate entity = super.get(plate);
		handleCascade(entity);
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param plate 查询条件
	 * @param plate.page 分页对象
	 * @return
	 */
	@Override
	public Page<Plate> findPage(Plate plate) {
		return super.findPage(plate);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param plate
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(Plate plate) {
		plate.setActivityId(plate.getType().equals("活动")?plate.getActivityId():null);
		plate.setRouteId(plate.getType().equals("栏目")?plate.getRouteId():null);
		super.save(plate);
	}
	
	/**
	 * 更新状态
	 * @param plate
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(Plate plate) {
		super.updateStatus(plate);
	}
	
	/**
	 * 删除数据
	 * @param plate
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(Plate plate) {
		super.delete(plate);
	}

	/*
	 * 获取限时爆款
	 * */
	public Map getRecommend(String plateId,String memberId){
		Plate plate = super.get(plateId);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Map map = new HashMap();
		map.put("title",plate.getTitle());
		map.put("subTitle",plate.getSubTitle());
		map.put("type",plate.getType());
		map.put("activityId",plate.getActivityId());
		if(StringUtils.isNotBlank( plate.getActivityId())){
			Activity activity = activityService.get(plate.getActivityId());
			Map activityMap = new HashMap();
			if(activity != null){
				activityMap.put("activityId", activity.getId());
				activityMap.put("brandId", activity.getBrandId());
				activityMap.put("thumbnail", activity.getThumbnail());
				activityMap.put("startTime", simpleDateFormat.format(activity.getStartTime()));
				activityMap.put("endTime", simpleDateFormat.format(activity.getEndTime()));
			}
			map.put("activity",activityMap);
		}
		map.put("routeId",plate.getRouteId());
		if(StringUtils.isNotBlank( plate.getRouteId())){
			Route route = routeService.get(plate.getRouteId());
			Map routeMap = new HashMap();
			if(route!=null) {
				routeMap.put("code", route.getCode());
				routeMap.put("name", route.getName());
				routeMap.put("path", route.getPath());
			}
			map.put("route",routeMap);
		}
		map.put("items",plateItemService.getRecommend(plateId,memberId));
		return map;
	}

	/*
	* 限时爆款
	* */
	public Map getLimitedRecommend(String memberId){
		return getRecommend("1215215779678441472",memberId);
	}

	/*
	 * 精选
	 * */
	public Map getBrandSelectionRecommend(String memberId){
		return getRecommend("1215215830609874944",memberId);
	}

	/*
	 * 专场
	 * */
	public Map getSpecialFieldRecommend(String memberId){
		return getRecommend("1215215889581789184",memberId);
	}

	/*
	 * 正在开售
	 * */
	public Map getHotActivityRecommend(String memberId){
		return getRecommend("1215215939137490944",memberId);
	}

	/*
	 * 即将
	 * */
	public Map getSaleActivityRecommend(String memberId){
		return getRecommend("1215215986952556544",memberId);
	}
}